<?php
require_once('../includes/frameworkajax.php');
if(@!$_GET['term'])
exit;

$term = @$_GET['term'];
$sql = "select ci_item||'-'||tipo||'@'||qtd_iten_anexo as ci_item, ds_item ||' R$'|| replace(nr_valor::character varying,'.',',') AS ds_item from (
select ci_item, ds_item, PI.NR_PRECO as nr_valor, 'I' as tipo,qtd_iten_anexo 
 from tethys.tb_itens i INNER join tethys.tb_preco_item pi on
		pi.cd_item=i.ci_item and pi.fl_ativa=TRUE
union 
select ci_pacote as ci_item, ds_pacote as ds_item, nr_valor, 'P' as tipo, 0 AS qtd_iten_anexo
from tethys.tb_pacotes) ip  
where ip.ds_item ilike '%{$term}%'
order by 2 asc";

$query = @Connection::query($sql);
$str = '[';
while($row = $query->fetch()){
	$str .= '{"id":"'.$row['ci_item'].'","label":"'.$row['ds_item'].'"},';
}
echo substr($str, 0, -1).']';
?>